package com.example.springbootdatamock.mock;

import com.apifan.common.random.RandomSource;
import com.apifan.common.random.source.AreaSource;
import com.example.springbootdatamock.mapper.HBaseMapperToCk;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
@RestController
public class HBaseMocker
        implements Runnable {
    @Value("${sleepMs.hbaseMockSleep}")
    private Integer mockSleep;
    @Autowired
    private HBaseMapperToCk mapper;
    private static final Logger log = LoggerFactory.getLogger(HBaseMocker.class);

    @RequestMapping("/mockHbaseToCK")
    public void run() {
        String[] brands = {"ZTE", "小米", "VIVO", "OPPO", "华为", "IPHONE", "荣耀", "REALME", "IQQO", "ONEPLUS"};
        AreaSource areaSource = RandomSource.areaSource();

        this.mapper.createTable();
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        log.info("hbase mocking start");

        while (true) {
            this.mapper.insertData(
                    RandomSource.dateTimeSource().randomPastTime(LocalDateTime.now(), 100L).format(df1),
                    brands[RandomUtils.nextInt(0, brands.length)],
                    areaSource.randomProvince(),
                    Integer.valueOf(RandomUtils.nextInt(10, 20)),
                    BigDecimal.valueOf(RandomUtils.nextInt(400000, 700000) / 100),
                    df2.format(RandomSource.dateTimeSource().randomPastTime(LocalDateTime.now(), 100L))
                    );


            try {
                Thread.sleep(this.mockSleep.intValue());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}



